package com.zk5_education.service_edu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zk5_education.ResultCode;
import com.zk5_education.config.ExceptionHandle.MyException;
import com.zk5_education.service_edu.entity.EduChapter;
import com.zk5_education.service_edu.entity.EduVideo;
import com.zk5_education.service_edu.entity.chapter.OneChapter;
import com.zk5_education.service_edu.entity.chapter.TwoChapter;
import com.zk5_education.service_edu.mapper.EduChapterMapper;
import com.zk5_education.service_edu.service.EduChapterService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zk5_education.service_edu.service.EduVideoService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-08-08
 */
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {

    @Autowired
    EduVideoService eduVideoService;
    
    @Override
    public List<OneChapter> getallChapter(String courseId) {
        List<OneChapter> list=new ArrayList<>();
    //查询章节
        QueryWrapper<EduChapter> qy=new QueryWrapper<>();
        qy.orderByAsc("sort","id");
        qy.eq("course_id",courseId);
        List<EduChapter> Onechapter = baseMapper.selectList(qy);
        //查询小节

        QueryWrapper<EduVideo> qy1=new QueryWrapper<>();
        qy.orderByDesc("sort","id");
        qy.eq("course_id",courseId);
        List<EduVideo> Twochapter = eduVideoService.list(qy1);
        //封装章节和小节
        for(EduChapter eduChapter :Onechapter){
            OneChapter oneChapter=new OneChapter();
            BeanUtils.copyProperties(eduChapter,oneChapter);
            List<TwoChapter > list1=new ArrayList<>();
            for(EduVideo ev:Twochapter){
                if(ev.getChapterId().equals(eduChapter.getId())){
                    TwoChapter to=new TwoChapter();
                    BeanUtils.copyProperties(ev,to);
                    list1.add(to);
                }
            }
            oneChapter.setChildren(list1);
            list.add(oneChapter);
        }
        return list;

    }

    @Override
    public boolean deleteChapter(String chapterid) {
        QueryWrapper<EduVideo> qy=new QueryWrapper<>();
        qy.eq("Chapter_id",chapterid);
        int count = eduVideoService.count(qy);
        if(count<=0){
            baseMapper.deleteById(chapterid);
            return true;
        }
        return false;

    }
}
